package com.ruan.handler;

import com.ruan.entity.R;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName:GlobalExceptionHandler
 * @Author:阮昇
 * @Description:全局异常处理
 * @Datetime: 2023/9/7 16:47
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(BindException.class)
    public R<Void> handleBindException(BindException exception) {
        List<FieldError> allErrors = exception.getFieldErrors();
        StringBuilder sb = new StringBuilder();
        for (FieldError errorMessage : allErrors) {
            sb.append(errorMessage.getDefaultMessage()).append("  ");
        }
        return new R<>(100,sb.toString());
    }
}
